package com.njitzx.service.impl;

import com.njitzx.constant.UserConstant;
import com.njitzx.entity.dto.PassWordVO;
import com.njitzx.entity.dto.UpdateUserVO;
import com.njitzx.entity.po.User;
import com.njitzx.entity.vo.UserInfoVO;
import com.njitzx.entity.vo.UserQueryDTO;
import com.njitzx.entity.vo.UserVO;
import com.njitzx.exception.LoginErrorException;
import com.njitzx.mapper.UserMapper;
import com.njitzx.service.UserService;
import com.njitzx.util.BaseContext;
import com.njitzx.util.PwdUtil;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 周鑫
 * @since 2024-08-20
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    /**
     * 用户表分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param user
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, UserVO user) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserQueryDTO> userList = userMapper.select(user);
        PageInfo<UserQueryDTO> pageInfo = new PageInfo<>(userList);

        pageInfo.getList().forEach(System.out::println);

        Map<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("pageNum", pageInfo.getPageNum());
        map.put("users", pageInfo.getList());
        return map;
    }

    /**
     * 根据主键查询用户表
     *
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public UserQueryDTO findById(Serializable id) {
        return userMapper.selectById(id);
    }

    /**
     * 用户表新增
     *
     * @param user
     */
    @Override
    public void add(User user) {
        user.setCreateTime(new Date());
        user.setMgState(UserConstant.USER_ENABLE);
        user.setRoleId(UserConstant.USER_ROLE_DEFAULT);
        //密码进行加密
        String password=user.getPassword();
        System.out.println(password);
        user.setPassword(PwdUtil.encrypt(password));
        userMapper.insert(user);
    }

    /**
     * 修改用户表
     *
     * @param user
     */
    @Override
    public void modify(User user) {
        userMapper.update(user);
    }

    /**
     * 根据主键删除用户表
     *
     * @param id
     */
    @Override
    public void removeById(Serializable id) {
        if (((String) id).contains(",")) {
            userMapper.deleteByIds(((String) id).split(","));
        } else {
            userMapper.deleteById(id);
        }
    }

    @Override
    public void updateStatus(Integer id, Integer state) {
        userMapper.updateStatus(id, state);
    }

    @Override
    public void updateRoleId(Integer id, Integer roleId) {
        userMapper.updateUserRoleId(roleId, id);
    }

    @Override
    public UserInfoVO getUserInfo() {
        Integer id= BaseContext.getCurrentId();
        return  userMapper.getUserInfo(id);
    }

    @Override
    public void changePassword(Integer id, PassWordVO passWordVO) {
        UserInfoVO userInfo = userMapper.getUserInfo(id);
        if (!PwdUtil.encrypt(passWordVO.getPassword()).equals(userInfo.getPassword())){
            throw new LoginErrorException("初始密码不正确");
        }
       String newPassword=PwdUtil.encrypt(passWordVO.getNewPassword());
        userMapper.updatePassWord(id,newPassword);
    }

    @Override
    public void updateInfo(Integer id, UpdateUserVO updateUserVO) {
        userMapper.updateInfo(id,updateUserVO);
    }
}
